package com.wangyp.tower.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangyp.tower.common.exception.TowerException;
import com.wangyp.tower.common.helper.JwtHelper;
import com.wangyp.tower.common.result.ResultCodeEnum;
import com.wangyp.tower.model.user.UserInfo;
import com.wangyp.tower.user.mapper.UserInfoMapper;
import com.wangyp.tower.user.service.UserInfoService;
import com.wangyp.tower.vo.user.LoginVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Wangyp
 * @Date: 2021/4/24 8:55
 * @Description: *
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService{

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登录接口
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> loginUser(UserInfo user) {
        // 从User中 获取输入的用户名 和 密码
        String userName = user.getName();
        Integer password = user.getPassword();

        // 判断是否为空
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            // 参数不正确
            throw new TowerException(ResultCodeEnum.PARAM_ERROR);
        }

        // 判断是否是第一次登录：根据手机号查询数据库， 如果不存在相同手机号就是第一次登录
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", userName);
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);

        if (userInfo == null) {
            // 第一次登录， 需注册
            userInfo = new UserInfo();
            userInfo.setName(userName);
            userInfo.setPassword(password);
            userInfo.setStatus(1);
            baseMapper.insert(userInfo);
        }

        // 校验是否被禁用
        if (userInfo.getStatus() == 0) {
            // 用户已被禁用
            throw new TowerException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }

        // 不是第一次， 直接登录

        // 返回登录信息, 用户名, token信息
        Map<String, Object> map = new HashMap<>();
        String name = userInfo.getName();
        if (StringUtils.isEmpty(name)) {
            name = userInfo.getNickName();
        }
        if (StringUtils.isEmpty(name)) {
            name = userInfo.getPhone();
        }
        map.put("name", name);

        //  token生成
        String token = JwtHelper.createToken(userInfo.getId(), name);
        map.put("token", token);
        map.put("roles", "admin");
        return map;
    }

    /**
     * 姓名， 密码登录
     * @param name
     * @param password
     * @return
     */
    @Override
    public Map<String, Object> loginbyname(String name, Integer password) {
        // 判断是否为空
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
            // 参数不正确
            throw new TowerException(ResultCodeEnum.PARAM_ERROR);
        }

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);

        if (userInfo == null) {
            // 第一次登录， 需注册
            userInfo = new UserInfo();
            userInfo.setName(name);
            userInfo.setPassword(password);
            userInfo.setStatus(1);
            baseMapper.insert(userInfo);
        }

        // 返回登录信息, 用户名, token信息
        Map<String, Object> map = new HashMap<>();
        String name1 = userInfo.getName();
        if (StringUtils.isEmpty(name)) {
            name1 = userInfo.getNickName();
        }
        if (StringUtils.isEmpty(name)) {
            name1 = userInfo.getPhone();
        }
        map.put("name", name1);

        //  token生成
        String token = JwtHelper.createToken(userInfo.getId(), name);
        map.put("token", token);
        map.put("roles", "admin");
        return map;
    }


    @Override
    public Map<String, Object> getUserInfo(String name) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);
        System.out.println(userInfo.toString());
        Map<String, Object> map = new HashMap<>();
        String userInfoName = userInfo.getName();

        map.put("name", userInfoName);
        map.put("roles", "[admin]");
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return map;
    }

}
